package com.qianfeng.lingshijia;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.util.List;

import custom.GoodsNumberReadView;
import entity.GoodsDetailPageEntity;
import utils.Constants;
import utils.DownloadUtils;

public class GoodsDetailPageActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout linear;
    private ImageView image;
    private TextView tvTitle;
    private TextView tvPrice, tvAdd;
    private RadioButton rbAdd, rbReduce;
    private GoodsNumberReadView numReadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_detail_page);
        init();
        Intent intent = getIntent();
        int productId = intent.getIntExtra("productId", -1);
        int sellerId = intent.getIntExtra("sellerId", -1);
        int goodsType = intent.getIntExtra("goodsType", -1);
        if (productId != -1 && sellerId != -1 && goodsType != -1) {
            String path = Constants.URL.GOODS_DETAIL_PAGE_URL + productId
                    + "&sellerId=" + sellerId + "&type=" + goodsType;
            DownloadUtils.getJson(path, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {

                    Log.e("GoodsDetailPageActivity", "onSuccess: " + responseInfo.result);
                    Gson gson = new Gson();
                    GoodsDetailPageEntity goodsDetailEntity = gson.fromJson(responseInfo.result, GoodsDetailPageEntity.class);
                    if (goodsDetailEntity.getData() != null) {
                        DownloadUtils.LoadPicture(goodsDetailEntity.getData().getImgUrl(), image);
                        tvTitle.setText(goodsDetailEntity.getData().getTitle());
                        tvPrice.setText(goodsDetailEntity.getData().getPrice());

                        List<GoodsDetailPageEntity.DataEntity.DescriptionEntity> descList = goodsDetailEntity.getData().getDescription();
                        if (descList != null) {
                            for (int i = 0; i < descList.size(); i++) {
                                GoodsDetailPageEntity.DataEntity.DescriptionEntity descEntity = descList.get(i);
                                ImageView images1 = new ImageView(GoodsDetailPageActivity.this);
                                images1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                images1.setAdjustViewBounds(true);
                                images1.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                DownloadUtils.LoadPicture(descEntity.getPicUrl(), images1);
                                linear.addView(images1);
                            }
                        }
                    }else {
                        Toast.makeText(GoodsDetailPageActivity.this, "亲，数据加载失败，请重试", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(HttpException error, String msg) {
                    Toast.makeText(GoodsDetailPageActivity.this, "数据下载失败", Toast.LENGTH_LONG).show();
                }
            });
        } else {
            Toast.makeText(this, "不好意思，加载数据错误啦！亲。", Toast.LENGTH_LONG).show();
        }

    }

    public void init() {
        linear = (LinearLayout) findViewById(R.id.id_linear);
        image = (ImageView) findViewById(R.id.id_detail_image);
        tvTitle = (TextView) findViewById(R.id.id_detail_title);
        tvPrice = (TextView) findViewById(R.id.id_detail_price);
        tvAdd = (TextView) findViewById(R.id.tv_add);
        rbAdd = (RadioButton) findViewById(R.id.id_add);
        rbAdd.setOnClickListener(this);
        rbReduce = (RadioButton) findViewById(R.id.id_reduce);
        rbReduce.setOnClickListener(this);
        numReadView = (GoodsNumberReadView) findViewById(R.id.numReadView);
    }

    public int n, i = 1;

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.id_add:
                ++n;
                tvAdd.setVisibility(View.VISIBLE);
                rbReduce.setVisibility(View.VISIBLE);
                if (i < n) {
                    tvAdd.setText(i + "");
                    i++;
                    numReadView.changeMessCount(true);
                }

                break;
            case R.id.id_reduce:
                --i;
                tvAdd.setText(i + "");
                if (i == 0) {
                    tvAdd.setVisibility(View.GONE);
                    rbReduce.setVisibility(View.GONE);

                }
                numReadView.changeMessCount(false);

                break;
        }
    }

    public void backToActivity(View view) {
        finish();
    }
}
